package com.example.springbootdeom.contrlloer;

import com.example.springbootdeom.entity.User;
import com.example.springbootdeom.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    //登录方法
    @GetMapping("/login.action")
    @ResponseBody
    public String login(String name, String password, HttpServletRequest request){
//        User user=userService.login(name,password);

        System.out.println("输入的name:"+name+"输入的pwd:"+password);
        User user=userService.login(name,password);

       if(user!=null)
       {

           String id=user.getStatus();
           request.getSession().setAttribute("user",user);
           HttpSession session =request.getSession();
           session.setAttribute("user",user);
           switch (id)
           {
               case "2":System.out.println("学生");
                        return "2";
//                        break;
               case "1":System.out.println("老师");
                   return "1";

               case "0":System.out.println("管理员");
                    return "0";

           }
       }
       else{
           return "error";
       }

        return null;
    };
    //根据id查询用户
    @GetMapping("getEUser.action")
    public String getUSer(Integer id){
        User user =userService.getUser(id);
        if(user!=null){return "nonull";}
        else{
            return "null";
        }

    }
}
